package com.gui.spring.aop.jdk;

import java.lang.reflect.Proxy;
import java.util.Arrays;

public class JdkProxyApiDemo {

    public static void main(String[] args) {
        ClassLoader classLoader = JdkProxyApiDemo.class.getClassLoader();
        Target target = (Target) Proxy.newProxyInstance(classLoader, new Class[]{Target.class}, (p, method, arguments) -> {
            System.out.println("前置通知，打印入参：" + Arrays.toString(arguments));
            Object result = method.invoke(new TargetService(), arguments);
            System.out.println("前置通知，打印返回值：" + Arrays.toString(arguments));
            return result;
        });
        target.foo(1);
        System.out.println(target.getClass());
    }

    static interface Target{
        public String foo(Integer type);
    }

    static class TargetService implements Target{

        @Override
        public String foo(Integer type) {
            System.out.println("foo..." + type);
            return "success";
        }
    }
}
